var type = GetQueryString('type');
var orderInfor = GetQueryString('orderInfor');
var useCardsIdList = '';
var addressId = '';
var deliveryType = '';
var payType = '';
var allPrice = '';
var ifClickNo = '';
var useAbleNum = '';


$(function () {
    addressId = getItem('addressId');
    if(addressId==null||addressId==undefined||addressId==""){}else{
        $.ajax({
            url:'/user/getByAddressId.json',
            dataType:'json',
            data:{
                token:getItem('token'),
                addressId:addressId,
                companyCode:getItem('companyCode'),
            },
            type:'post',
            success:function (data) {
                if(data.code==1){
                    $('.name').html(data.datas.name);
                    $('.phone').html(data.datas.phone);
                    $('.address_detail').html(data.datas.city + data.datas.detail);
                }
            }
        })
    }

    if(getItem('deliveryType')==null||getItem('deliveryType')==undefined||getItem('deliveryType')==""){
        deliveryType = 2;
        payType = 1;
    }else{
        deliveryType = getItem('deliveryType');
        payType = getItem('payType');
        $('.way').html(decodeURI(getItem('payTypeHtml'))+' '+decodeURI(getItem('deliveryTypeHtml')));
    }
    if(type==1){
        doConfimOrder();
    }else{
        doConfimOrder2();
    }
})


var num = '',
    goodsId = '',
    goodsTypeValue = '',
    periodis = '';
//立即购买
function doConfimOrder() {
    orderInfor = JSON.parse(orderInfor);
    num = orderInfor.num;
    goodsId = orderInfor.goodsId;
    periodis = orderInfor.periodis;
    goodsTypeValue = orderInfor.goodsTypeValue;
    $.ajax({
        url: '/seckillGoodsC/confirmOrder.json',
        dataType: 'json',
        data: {
            token: getItem('token'),
            id:goodsId,
            companyCode:getItem('companyCode'),
        },
        type: 'post',
        beforeSend:function(){
            $('<div class="loading2"><span class="mui-spinner"></span></div>').appendTo($('body'));
        },
        success: function (data) {
            $('.loading2').hide();
            if (data.code == 0) {
                if(data.msg=="请添加地址！"){
                    mui.confirm( '您还没有收获地址，先去先设置一个吧',' ', ['取消', '去设置'], function(e) {
                        if (e.index == 1) {
                            window.location.href="add_address2.html";
                        } else {
                            window.location.href=history.go(-2);
                        }
                    })
                    return;
                }
                mui.toast(data.msg);
                return;
            }
            if (data.code == -1) {
                window.location.href = "login.html";
            }
            doData(data);
        }
    })
}

function doData(b) {
    if((deliveryType==1&&payType==1)||(deliveryType==1&&payType==2)){
        var shopAddress = JSON.parse(getItem('shopAddress'));
        $('.user_info').html('联系人<span class="name">'+shopAddress.name+'</span><span class="phone">'+shopAddress.phone+'</span>');
        $('.address_detail').html(shopAddress.detail);
    }else{
        if(b.addressType==2){
            if(addressId==null||addressId==undefined||addressId==""){
                $('.name').html(b.address.name);
                $('.phone').html(b.address.phone);
                $('.address_detail').html(b.address.city + b.address.detail);
                addressId = b.address.id;
            }else{}
        }else if(b.addressType==1){
            $('.name').html('请填写详细信息');
            $('.phone').html('');
            $('.address_detail').html(b.addressContent);
        }
    }

        $('<div class="goods">\n' +
            '<div class="detailBox mui-clearfix">\n' +
            '<div class="goods-img-box">\n' +
            '<img src="'+b.goods.imgUrl.split(',')[0]+'">\n' +
            '</div> <div class="goods-product-box">\n' +
            '<p class="goodsName">'+b.goods.goodName+'</p>\n' +
            '<span class="price">￥'+b.goods.goodsPrice+'<i>×'+b.goods.num+'</i></span>\n' +
            '</div></div></div><div class="line"></div>').appendTo($('.goodsBox'))

    $('#totalPrice').html('￥'+b.totalMoney);
    $('.num').html(b.goods.num+'件商品');
    $('#price1').html('商品：￥'+b.totalMoney);
    $('#price2').html('优惠：￥'+b.saveMoney);
}


//去结算
function doConfimOrder2() {
    $.ajax({
        url:'/purchaseOrderC/cartsBuy.json',
        dataType:'json',
        data:{
            token:getItem('token'),
            companyCode:getItem('companyCode'),
        },
        type:'post',
        beforeSend:function(){
            $('<div class="loading2"><span class="mui-spinner"></span></div>').appendTo($('body'));
        },
        success:function (data) {
            $('.loading2').hide();
            if(data.code==0){
                mui.toast(data.msg);
                return;
            }
            if(data.code==-1){
                window.location.href="login.html"
            }
            doData(data);
        }
    })
}

//提交订单
document.getElementById('submit').addEventListener('tap',function () {
    if(type==1){
        //立即购买提交
        $.ajax({
            url:'/seckillGoodsC/submitOrder.json',
            dataType:'json',
            data:{
                token:getItem('token'),
                seckillGoodsId:goodsId,
                addressId:addressId,
                memo:$('#tips').val(),
                deliveryType:2,
                payType:1,
                companyCode:getItem('companyCode'),
            },
            beforeSend: function () {
                $('<div class="loading2"><span class="mui-spinner"></span></div>').appendTo($('body'));
            },
            success:function (data) {
                $('.loading2').hide();
                if(data.code==1){
                    removeItem('addressId');
                    var code= data.pCode;
                    var payMoney = data.payMoney;
                    if((deliveryType==2&&payType==1)||(deliveryType==1&&payType==1)){
                        window.location.href="choosePayWay.html?payMoney="+payMoney+'&code='+code;
                    }else if(deliveryType==1&&payType==2){
                        window.location.href="confirmSuccess.html?payMoney="+payMoney+'&code='+code;
                    }
                }
                if(data.code==0||data.code==3){
                    mui.toast(data.msg);
                    return;
                }
                if(data.code==-1){
                    window.location.href="login.html"
                }
            }
        })
    }else{
        //购物车提交
        $.ajax({
            url:'/purchaseOrderC/submitOrder.json',
            dataType:'json',
            data:{
                token:getItem('token'),
                type:'2',
                addressId:addressId,
                userCardsId:useCardsIdList,
                deliveryType:deliveryType,
                payType:payType,
                memo:$('#tips').val(),
                companyCode:getItem('companyCode'),
            },
            beforeSend: function () {
                $('<div class="loading2"><span class="mui-spinner"></span></div>').appendTo($('body'));
            },
            success:function (data) {
                $('.loading2').hide();
                if(data.code==1){
                    removeItem('addressId');
                    var payMoney = data.payMoney;
                    var code= data.pCode;
                    if((deliveryType==2&&payType==1)||(deliveryType==1&&payType==1)){
                        window.location.href="choosePayWay.html?payMoney="+payMoney+'&code='+code+'&deliverType='+deliveryType+'&payType='+payType;
                    }else if(deliveryType==1&&payType==2){
                        window.location.href="confirmSuccess.html?payMoney="+payMoney+'&code='+code;
                    }
                }
                if(data.code==0||data.code==3){
                    mui.toast(data.msg);
                    return;
                }
                if(data.code==-1){
                    window.location.href="login.html"
                }
            }
        })
    }
})

//地址
document.getElementById('setUrl').addEventListener('tap',function () {
    setItem('setUrl',window.location.href);
})

mui('.mui-scroll-wrapper').scroll({
    deceleration: 0.0005 //flick 减速系数，系数越大，滚动速度越慢，滚动距离越小，默认值0.0006
});